package com.zzgc.modules.service.impl;

import com.zzgc.common.dao.*;
import com.zzgc.common.entity.*;
import com.zzgc.common.response.Result;
import com.zzgc.common.utils.MD5Util;
import com.zzgc.common.utils.StringUtils;
import com.zzgc.modules.param.AuthUserParam;
import com.zzgc.modules.param.AuthUserSchoolParam;
import com.zzgc.modules.service.AuthUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户表(AuthUser)表服务实现类
 *
 * @author makejava
 * @since 2022-12-19 17:26:24
 */
@Service("authUserService")
public class AuthUserServiceImpl implements AuthUserService {
    @Autowired
    private AuthUserDao authUserDao;
    @Autowired
    private AuthUserRoleDao authUserRoleDao;
    @Autowired
    private AuthDepartDao authDepartDao;
    @Autowired
    private SysDictItemDao sysDictItemDao;
    @Autowired
    private SchoolDao schoolDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AuthUser queryById(Long id) {
        return this.authUserDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param authUser 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public List<AuthUser> queryByPage(AuthUser authUser) {
        List<AuthUser> authUsers = this.authUserDao.queryAllByLimit(authUser);
        if (authUsers.size() == 0) {
            return new ArrayList<>();
        }
        long total = this.authUserDao.count(authUser);
        authUsers.get(0).setTotal(total);
        return authUsers;
    }


    /**
     * 分页查询
     *
     * @param authDepart 筛选条件
     * @return 查询结果
     */
    @Override
    public List<AuthUser> queryByDepart(AuthDepart authDepart) {
        List<AuthUser> authUsers = this.authUserDao.queryByDepart(authDepart);
        if (authUsers.size() == 0) {
            return new ArrayList<>();
        }
        authDepart.setSize(Integer.MAX_VALUE);
        List<AuthUser> authUsers1 = this.authUserDao.queryByDepart(authDepart);
        authUsers.get(0).setTotal(authUsers1.size());
        return authUsers;
    }

    /**
     * 新增数据 添加学校角色的关系表
     *
     * @param authUser 实例对象
     * @return 实例对象
     */
    @Override
    public AuthUser insert(AuthUser authUser) {
        this.authUserDao.insert(authUser);
        AuthUserRole authUserRole = new AuthUserRole();
        authUserRole.setAUserId(authUser.getId());
        authUserRole.setARoleId(5L); // 创建学校角色
        authUserRole.setACreateDate(new Date());
        // 添加角色用户关系表
        authUserRoleDao.insert(authUserRole);
        return authUser;
    }

    @Override
    @Transactional
    public AuthUserSchoolParam insertUser(AuthUserSchoolParam userSchoolParam) {
        AuthDepart authDepart = new AuthDepart();
        authDepart.setAIdBak(userSchoolParam.getDistrict());
        authDepart.setALevel(3);
        Date date = new Date();
        // 获取父级id
        List<AuthDepart> authDeparts = authDepartDao.queryAllByLimit(authDepart);
        // 获取地区代码
        SysDictItem sysDictItem = sysDictItemDao.queryByName(userSchoolParam.getDistrict());
        if (authDeparts.size() != 0) {
            authDepart = authDeparts.get(0);
        }else {
            authDepart.setId(1L);
        }
        // 添加部门信息
        AuthDepart depart = new AuthDepart();
        depart.setAName(userSchoolParam.getSchoolName());
        depart.setANote(userSchoolParam.getAddress());
        depart.setAParentIdBak(userSchoolParam.getCity());
        depart.setAIdBak(userSchoolParam.getDistrict());
        depart.setAParentId(authDepart.getId());
        depart.setARegionId(Long.valueOf(sysDictItem.getSCode()));
        depart.setALevel(4);
        depart.setASort(sysDictItem.getSSort());
        depart.setACreateDate(date);
        authDepartDao.insert(depart);
        // 添加学校信息
        School school = new School();
        school.setSAddress(userSchoolParam.getAddress());
        school.setSName(depart.getAName());
        school.setSDepartId(depart.getAParentId()); // 父级id
        school.setSSchoolDepartId(depart.getId());
        school.setSType(userSchoolParam.getSchoolType());
        school.setSArea(String.valueOf(sysDictItem.getSCode()));
        school.setSCreateDate(date);
        schoolDao.insert(school);
        // 添加用户信息
        AuthUser authUser = new AuthUser();
        // 密码加密， 默认密码123456
        authUser.setAPassword(MD5Util.getMD5PWD("123456"));
        authUser.setAName(userSchoolParam.getName());
        authUser.setAAccount(userSchoolParam.getAccount());
        authUser.setAMobile(userSchoolParam.getMobile());
        // 通过此路径添加的都是主账号
        authUser.setAState(1);
        // 添加创建时间
        authUser.setACreateDate(date);
        authUser.setABankStatus("0");
        authUser.setADepartId(depart.getId());
        authUser.setASchoolId(school.getId());
        // 添加用户和角色关系表
        this.insert(authUser);

        return userSchoolParam;
    }

    /**
     *  添加区县，部门，用户信息
     * @ClassName AuthUserService
     * @Date 2023/5/8  17:02
     * @Version 1.0
     */
    @Override
    @Transactional
    public AuthUserSchoolParam insertUserDepart(AuthUserSchoolParam userSchoolParam) {
        AuthDepart authDepart = new AuthDepart();
        authDepart.setAIdBak(userSchoolParam.getCity());
        authDepart.setALevel(2);
        // 获取父级id
        List<AuthDepart> authDeparts = authDepartDao.queryAllByLimit(authDepart);
        // 获取地区代码
        SysDictItem sysDictItem = sysDictItemDao.queryByName(userSchoolParam.getDistrict());
        // 添加部门信息
        Date date = new Date();
        AuthDepart depart = new AuthDepart();
        depart.setAName(userSchoolParam.getCity() + userSchoolParam.getDistrict() + "教育局");
        depart.setANote(StringUtils.isEmpty(userSchoolParam.getAddress()) ? depart.getAName() : userSchoolParam.getAddress());
        depart.setAParentIdBak(userSchoolParam.getCity());
        depart.setAIdBak(userSchoolParam.getDistrict());
        depart.setAParentId(authDeparts.get(0).getId());
        depart.setARegionId(Long.valueOf(sysDictItem.getSCode()));
        depart.setALevel(3);
        depart.setASort(sysDictItem.getSSort());
        depart.setACreateDate(date);
        authDepartDao.insert(depart);

        // 添加用户信息
        AuthUser authUser = new AuthUser();
        // 密码加密， 默认密码123456
        authUser.setAPassword(MD5Util.getMD5PWD("123456"));
        authUser.setAName(userSchoolParam.getName());
        authUser.setAAccount(userSchoolParam.getAccount());
        authUser.setAMobile(userSchoolParam.getMobile());
        // 通过此路径添加的都是主账号
        authUser.setAState(1);
        // 添加创建时间
        authUser.setACreateDate(date);
        authUser.setABankStatus("0");
        authUser.setADepartId(depart.getId());
        // 添加用户和角色关系表
        this.authUserDao.insert(authUser);
        AuthUserRole authUserRole = new AuthUserRole();
        authUserRole.setAUserId(authUser.getId());
        authUserRole.setARoleId(4L); // 创建区县角色
        authUserRole.setACreateDate(new Date());
        // 添加角色用户关系表
        authUserRoleDao.insert(authUserRole);

        return userSchoolParam;
    }

    /**
     * 修改数据
     *
     * @param authUser 实例对象
     * @return 实例对象
     */
    @Override
    public AuthUser update(AuthUser authUser) {
        this.authUserDao.update(authUser);
        return this.queryById(authUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean deleteById(Long id) {
        AuthUser authUser = this.authUserDao.queryById(id);
        // 查询部门信息
        AuthDepart authDepart = authDepartDao.queryById(authUser.getADepartId());
        List<AuthDepart> authDeparts = authDepartDao.queryByParentId(authDepart.getId());
        if (StringUtils.isNotEmpty(authDeparts)) {
            throw new RuntimeException("区县下存在学校，无法删除区县！");
        }
        // 查询权限信息
        AuthUserRole authUserRole = authUserRoleDao.queryByUserId(id);
        // 删除部门表信息
        authDepartDao.deleteById(authDepart.getId());
        // 删除角色表数据
        authUserRoleDao.deleteById(authUserRole.getId());
        // 删除用户数据
        return this.authUserDao.deleteById(id) > 0;
    }

    /**
     * 通过账号和手机号查询用户
     *
     * @param aAccount 账号
     * @param aMobile 手机号
     * @return 实例对象
     */
    @Override
    public List<AuthUser> queryAllByNamePwd(String aAccount, String aMobile) {
        return this.authUserDao.queryAllByNamePwd(aAccount,aMobile);
    }
}
